package com.hxh.tess4j.util;

import java.io.*;
import java.net.URL;

/**
 * @author huangxunhui
 * Date: Created in 2019-04-12 23:23
 * Utils: Intellij Idea
 * Description:
 */
public class DownFileUtil {

    private static final String FILE_PATH = "/Users/code/IdeaProjects/tess4j/src/main/resources/img";

    public static void downloadPicture(String urlList , String imageName ) throws IOException {
        URL url = new URL(urlList);

        DataInputStream dataInputStream = new DataInputStream(url.openStream());

        FileOutputStream fileOutputStream = new FileOutputStream(new File(FILE_PATH , imageName));

        ByteArrayOutputStream output = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];
        int length;

        while ((length = dataInputStream.read(buffer)) > 0) {
            output.write(buffer, 0, length);
        }

        fileOutputStream.write(output.toByteArray());
        dataInputStream.close();
        fileOutputStream.close();
    }

    public static void mkDir(String dirName){
        File file = new File(FILE_PATH+dirName);
        if(!file.exists()){
            file.mkdir();
        }
    }
}

